package com.zhida.smartcontrol.utils;

import android.content.Context;

import com.jay.tools.utils.ToastUtil;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;

import java.util.Timer;
import java.util.TimerTask;

import static android.view.KeyEvent.KEYCODE_BACK;

public class ClickUtil {
    public static final int DELAY = 5000;
    private static long lastClickTime = 0;

    public static boolean isNotFastClick(Context context) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClickTime > DELAY) {
            lastClickTime = currentTime;
            return true;
        } else {
            ToastUtil.toast(context, "禁止连续点击");
            return false;
        }
    }

    /**
     * TimkerTask 方式实现
     */
    public static void timerTask(Timer timer) {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                timer.cancel();
//                sendKeyCode1(KEYCODE_BACK);
            }
        };
        timer.schedule(task, 5000, 5000);
    }

    /**
     * 用Runtime模拟按键操作
     *
     * @param keyCode 按键事件(KeyEvent)的按键值
     */
    public static void sendKeyCode1(int keyCode) {
        try {
            String keyCommand = "input keyevent " + keyCode;
            // 调用Runtime模拟按键操作
            Runtime.getRuntime().exec(keyCommand);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
